﻿//-----------------------------------------------------------------------
// <copyright file="ParamterCheckExtensions.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: ParamterCheckExtensions.cs
// * history : created by qinchaoyue 2017-10-16 04:04:52
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Slwy.Utility.Extensions
{
    using Slwy.Utility.SlwyException;

    public static class ParamterCheckExtensions
    {
        /// <summary>
        /// 验证指定值的断言<paramref name="assertion"/>是否为真，如果不为真，抛出指定消息<paramref name="message"/>的指定类型<typeparamref name="TException"/>异常
        /// </summary>
        /// <typeparam name="TException">异常类型</typeparam>
        /// <param name="assertion">要验证的断言。</param>
        /// <param name="message">异常消息。</param>
        private static void Require<TException>(bool assertion, string message)
            where TException : Exception
        {
            if (assertion)
            {
                return;
            }
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentNullException("message");
            }

            TException exception = (TException)Activator.CreateInstance(typeof(TException), message);
            throw exception;
        }

        /// <summary>
        /// 检查参数不能为空引用，否则抛出<see cref="SlwyArgumentNullException"/>异常。
        /// </summary>
        /// <typeparam name="T"> 入参类型 </typeparam>
        /// <param name="value"> the value </param> 
        /// <param name="paramName"> 参数名称 </param>
        /// <exception cref="ArgumentNullException"> 参数为空错误 </exception>
        public static void CheckNotNull<T>(this T value, string paramName)
            where T : class
        {
            Require<ArgumentNullException>(value != null, $"参数“{paramName}”不能为空引用。");
        }

        /// <summary>
        /// 检查参数不能为空引用
        /// 检查参数不能为空引用，否则抛出<see cref="SlwyArgumentNullException"/>异常。
        /// </summary>
        /// <typeparam name="T"> 入参类型 . </typeparam>
        /// <typeparam name="TOut"> 出参类型 . </typeparam>
        /// <param name="value"> the value . </param> 
        /// <param name="outObj"> 返回数据 . </param>
        /// <exception cref="SlwyArgumentNullException"> 参数为空错误 </exception>
        public static void ThrowIsNull<T, TOut>(this T value, TOut outObj)
            where T : class
        {
            if (value == null)
            {
                throw new SlwyArgumentNullException(outObj);
            }
        }

        public static void ThrowIsNull<T>(this T value, int busCode = 0, string msg = "")
            where T : class
        {
            value.ThrowIsNull(new { BusCode = busCode, Msg = msg });
        }

        public static void ThrowIsCountZero<T, TOut>(this T value, TOut outObj)
            where T : IList
        {
            if (value.Count == 0)
            {
                throw new SlwyArgumentNullException(outObj);
            }
        }

        public static void ThrowIsCountZero<T>(this T value, int busCode = 0, string msg = "")
            where T : IList
        {
            value.ThrowIsCountZero(new { BusCode = busCode, Msg = msg });
        }

        public static void ThrowIsNullCountZero<T, TOut>(this T value, TOut outObj)
            where T : IList
        {
            if (value != null && value.Count != 0)
            {
                return;
            }

            throw new SlwyArgumentNullException(outObj);
        }

        public static void ThrowIsNullCountZero<T>(this T value, int busCode = 0, string msg = "")
            where T : IList
        {
            value.ThrowIsNullCountZero(new { BusCode = busCode, Msg = msg });
        }
    }
}